﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.CompilerServices;

namespace 图片裁剪压缩工具
{
    public class ImageManager
    {
        private PictureBox PictureBox { get; }
        private ImageList ImageList { get; }
        public Action<object, Image?> ImageChange { get; set; }
        public ImageManager(PictureBox pictureBox)
        {
            PictureBox = pictureBox;
            ImageList = new ImageList();
            ImageChange = delegate (object o, Image? i)
            {
            };
        }
        public Image? Current => ImageList.Current;

        internal void Reset(Image image, bool dispose=true)
        {
            List<Image> images = ImageList.Images;
            PictureBox.Image = ImageList.Reset(image);
            ImageChange?.Invoke(PictureBox, Current);
            if (dispose)
            {
                ImageList.DisposeAll(images);
            }
        }

        internal void Cancel()
        {
            PictureBox.Image = ImageList.Cancel();
            ImageChange?.Invoke(PictureBox, Current);
        }

        internal void Add(Image? image)
        {
            PictureBox.Image =ImageList.Add(image);
            ImageChange?.Invoke(PictureBox, Current);
        }
        public void RotateFlip(RotateFlipType rotateFlipType)
        {
            Current?.RotateFlip(rotateFlipType);
            PictureBox.Invalidate();
        }
    }
}